//
//  InfoPlugin.m
//  J2NativeDemo
//
//  Created by chenliang on 2018/5/8.
//  Copyright © 2018年 yypt. All rights reserved.
//

#import "InfoPlugin.h"
#import "JSONWrapper.h"
#import <MapKit/MapKit.h>
#import "EnvConfig.h"

@interface InfoPlugin()<CLLocationManagerDelegate>
    
@end

@implementation InfoPlugin{
    CLLocationManager *_locationManager;
}

-(void)location:(NSString *)argument{
    [self location];
}

-(void)getVersion:(NSString *)argument{
    //NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSDictionary *dict = @{@"ret":@0,@"result":@{@"version":App_Version}};
    [self toSuccessCallbackAsString:[dict objectToJSONString]];
}

-(void)getRegistrationID:(NSString *)argument{
    NSString *pdsmuuid = [[NSUserDefaults standardUserDefaults]objectForKey:@"registrationID"];
    [self toSuccessCallbackAsString:pdsmuuid];
}

-(void)location{
    //开启定位
    _locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = 100.0f;
    if (@available(iOS 8.0, *)) {
        [_locationManager requestWhenInUseAuthorization];
    }
    [_locationManager startUpdatingLocation];
}
    //定位代理经纬度回调
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [_locationManager stopUpdatingLocation];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    NSDictionary *dict = @{@"ret":@0,@"result":@{@"longitude":[NSString stringWithFormat:@"%f",oldCoordinate.longitude],@"latitude":[NSString stringWithFormat:@"%f",oldCoordinate.latitude]}};
    NSLog(@"dict = %@",dict);
    NSString *json = [dict objectToJSONString];
    [self toSuccessCallbackAsString:json];
}
- (void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(nullable NSError *)error{
    NSLog(@"error = %@",error);
    NSString *json = [NSString stringWithFormat:@"{'ret':1,'result':'%@'}",error];
    [self toFailCallbackAsString:json];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"error = %@",error);
    NSString *json = [NSString stringWithFormat:@"{'ret':1,'result':'%@'}",error];
    [self toFailCallbackAsString:json];
}
@end
